﻿using System;
using HslCommunication.Core;

namespace HslCommunication.Profinet.Inovance
{
	/// <summary>
	/// 汇川PLC的辅助类，提供一些地址解析的方法<br />
	/// Auxiliary class of Yaskawa robot, providing some methods of address resolution
	/// </summary>
	// Token: 0x02000099 RID: 153
	public class InovanceHelper
	{
		// Token: 0x06000B8B RID: 2955 RVA: 0x000437C0 File Offset: 0x000419C0
		private static int CalculateStartAddress(string address)
		{
			bool flag = address.IndexOf('.') < 0;
			int result;
			if (flag)
			{
				result = int.Parse(address);
			}
			else
			{
				string[] array = address.Split(new char[]
				{
					'.'
				}, StringSplitOptions.RemoveEmptyEntries);
				result = int.Parse(array[0]) * 8 + int.Parse(array[1]);
			}
			return result;
		}

		/// <summary>
		/// 根据汇川PLC的地址，解析出转换后的modbus协议信息
		/// </summary>
		/// <param name="address">安川plc的地址信息</param>
		/// <param name="modbusCode">原始的对应的modbus信息</param>
		/// <returns>还原后的modbus地址</returns>
		// Token: 0x06000B8C RID: 2956 RVA: 0x00043814 File Offset: 0x00041A14
		public static OperateResult<string> PraseInovanceAMAddress(string address, byte modbusCode)
		{
			OperateResult<string> result;
			try
			{
				string str = string.Empty;
				OperateResult<int> operateResult = HslHelper.ExtractParameter(ref address, "s");
				bool isSuccess = operateResult.IsSuccess;
				if (isSuccess)
				{
					str = string.Format("s={0};", operateResult.Content);
				}
				bool flag = address.StartsWith("QX") || address.StartsWith("qx");
				if (flag)
				{
					result = OperateResult.CreateSuccessResult<string>(str + InovanceHelper.CalculateStartAddress(address.Substring(2)).ToString());
				}
				else
				{
					bool flag2 = address.StartsWith("Q") || address.StartsWith("q");
					if (flag2)
					{
						result = OperateResult.CreateSuccessResult<string>(str + InovanceHelper.CalculateStartAddress(address.Substring(1)).ToString());
					}
					else
					{
						bool flag3 = address.StartsWith("IX") || address.StartsWith("ix");
						if (flag3)
						{
							result = OperateResult.CreateSuccessResult<string>(str + "x=2;" + InovanceHelper.CalculateStartAddress(address.Substring(2)).ToString());
						}
						else
						{
							bool flag4 = address.StartsWith("I") || address.StartsWith("i");
							if (flag4)
							{
								result = OperateResult.CreateSuccessResult<string>(str + "x=2;" + InovanceHelper.CalculateStartAddress(address.Substring(1)).ToString());
							}
							else
							{
								bool flag5 = address.StartsWith("MW") || address.StartsWith("mw");
								if (flag5)
								{
									result = OperateResult.CreateSuccessResult<string>(str + address.Substring(2));
								}
								else
								{
									bool flag6 = address.StartsWith("M") || address.StartsWith("m");
									if (flag6)
									{
										result = OperateResult.CreateSuccessResult<string>(str + address.Substring(1));
									}
									else
									{
										bool flag7 = modbusCode == 1 || modbusCode == 15 || modbusCode == 5;
										if (flag7)
										{
											bool flag8 = address.StartsWith("SMX") || address.StartsWith("smx");
											if (flag8)
											{
												return OperateResult.CreateSuccessResult<string>(str + string.Format("x={0};", (int)(modbusCode + 48)) + InovanceHelper.CalculateStartAddress(address.Substring(3)).ToString());
											}
											bool flag9 = address.StartsWith("SM") || address.StartsWith("sm");
											if (flag9)
											{
												return OperateResult.CreateSuccessResult<string>(str + string.Format("x={0};", (int)(modbusCode + 48)) + InovanceHelper.CalculateStartAddress(address.Substring(2)).ToString());
											}
										}
										else
										{
											bool flag10 = address.StartsWith("SDW") || address.StartsWith("sdw");
											if (flag10)
											{
												return OperateResult.CreateSuccessResult<string>(str + string.Format("x={0};", (int)(modbusCode + 48)) + address.Substring(3));
											}
											bool flag11 = address.StartsWith("SD") || address.StartsWith("sd");
											if (flag11)
											{
												return OperateResult.CreateSuccessResult<string>(str + string.Format("x={0};", (int)(modbusCode + 48)) + address.Substring(2));
											}
										}
										result = new OperateResult<string>(StringResources.Language.NotSupportedDataType);
									}
								}
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				result = new OperateResult<string>(ex.Message);
			}
			return result;
		}

		// Token: 0x06000B8D RID: 2957 RVA: 0x00043BA4 File Offset: 0x00041DA4
		private static int CalculateH3UStartAddress(string address)
		{
			bool flag = address.IndexOf('.') < 0;
			int result;
			if (flag)
			{
				result = Convert.ToInt32(address, 8);
			}
			else
			{
				string[] array = address.Split(new char[]
				{
					'.'
				}, StringSplitOptions.RemoveEmptyEntries);
				result = Convert.ToInt32(array[0], 8) * 8 + int.Parse(array[1]);
			}
			return result;
		}

		/// <summary>
		/// 根据安川PLC的地址，解析出转换后的modbus协议信息，适用H3U系列
		/// </summary>
		/// <param name="address">安川plc的地址信息</param>
		/// <param name="modbusCode">原始的对应的modbus信息</param>
		/// <returns>还原后的modbus地址</returns>
		// Token: 0x06000B8E RID: 2958 RVA: 0x00043BF8 File Offset: 0x00041DF8
		public static OperateResult<string> PraseInovanceH3UAddress(string address, byte modbusCode)
		{
			OperateResult<string> result;
			try
			{
				string str = string.Empty;
				OperateResult<int> operateResult = HslHelper.ExtractParameter(ref address, "s");
				bool isSuccess = operateResult.IsSuccess;
				if (isSuccess)
				{
					str = string.Format("s={0};", operateResult.Content);
				}
				bool flag = modbusCode == 1 || modbusCode == 15 || modbusCode == 5;
				if (flag)
				{
					bool flag2 = address.StartsWith("X") || address.StartsWith("x");
					if (flag2)
					{
						return OperateResult.CreateSuccessResult<string>(str + (InovanceHelper.CalculateH3UStartAddress(address.Substring(1)) + 63488).ToString());
					}
					bool flag3 = address.StartsWith("Y") || address.StartsWith("y");
					if (flag3)
					{
						return OperateResult.CreateSuccessResult<string>(str + (InovanceHelper.CalculateH3UStartAddress(address.Substring(1)) + 64512).ToString());
					}
					bool flag4 = address.StartsWith("SM") || address.StartsWith("sm");
					if (flag4)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(2)) + 9216).ToString());
					}
					bool flag5 = address.StartsWith("S") || address.StartsWith("s");
					if (flag5)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 57344).ToString());
					}
					bool flag6 = address.StartsWith("T") || address.StartsWith("t");
					if (flag6)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 61440).ToString());
					}
					bool flag7 = address.StartsWith("C") || address.StartsWith("c");
					if (flag7)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 62464).ToString());
					}
					bool flag8 = address.StartsWith("M") || address.StartsWith("m");
					if (flag8)
					{
						int num = Convert.ToInt32(address.Substring(1));
						bool flag9 = num >= 8000;
						if (flag9)
						{
							return OperateResult.CreateSuccessResult<string>(str + (num - 8000 + 8000).ToString());
						}
						return OperateResult.CreateSuccessResult<string>(str + num.ToString());
					}
				}
				else
				{
					bool flag10 = address.StartsWith("D") || address.StartsWith("d");
					if (flag10)
					{
						return OperateResult.CreateSuccessResult<string>(str + Convert.ToInt32(address.Substring(1)).ToString());
					}
					bool flag11 = address.StartsWith("SD") || address.StartsWith("sd");
					if (flag11)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(2)) + 9216).ToString());
					}
					bool flag12 = address.StartsWith("R") || address.StartsWith("r");
					if (flag12)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 12288).ToString());
					}
					bool flag13 = address.StartsWith("T") || address.StartsWith("t");
					if (flag13)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 61440).ToString());
					}
					bool flag14 = address.StartsWith("C") || address.StartsWith("c");
					if (flag14)
					{
						int num2 = Convert.ToInt32(address.Substring(1));
						bool flag15 = num2 >= 200;
						if (flag15)
						{
							return OperateResult.CreateSuccessResult<string>(str + ((num2 - 200) * 2 + 63232).ToString());
						}
						return OperateResult.CreateSuccessResult<string>(str + (num2 + 62464).ToString());
					}
				}
				result = new OperateResult<string>(StringResources.Language.NotSupportedDataType);
			}
			catch (Exception ex)
			{
				result = new OperateResult<string>(ex.Message);
			}
			return result;
		}

		/// <summary>
		/// 根据安川PLC的地址，解析出转换后的modbus协议信息，适用H5U系列
		/// </summary>
		/// <param name="address">安川plc的地址信息</param>
		/// <param name="modbusCode">原始的对应的modbus信息</param>
		/// <returns>还原后的modbus地址</returns>
		// Token: 0x06000B8F RID: 2959 RVA: 0x000440B4 File Offset: 0x000422B4
		public static OperateResult<string> PraseInovanceH5UAddress(string address, byte modbusCode)
		{
			OperateResult<string> result;
			try
			{
				string str = string.Empty;
				OperateResult<int> operateResult = HslHelper.ExtractParameter(ref address, "s");
				bool isSuccess = operateResult.IsSuccess;
				if (isSuccess)
				{
					str = string.Format("s={0};", operateResult.Content);
				}
				bool flag = modbusCode == 1 || modbusCode == 15 || modbusCode == 5;
				if (flag)
				{
					bool flag2 = address.StartsWith("X") || address.StartsWith("x");
					if (flag2)
					{
						return OperateResult.CreateSuccessResult<string>(str + (InovanceHelper.CalculateH3UStartAddress(address.Substring(1)) + 63488).ToString());
					}
					bool flag3 = address.StartsWith("Y") || address.StartsWith("y");
					if (flag3)
					{
						return OperateResult.CreateSuccessResult<string>(str + (InovanceHelper.CalculateH3UStartAddress(address.Substring(1)) + 64512).ToString());
					}
					bool flag4 = address.StartsWith("S") || address.StartsWith("s");
					if (flag4)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 57344).ToString());
					}
					bool flag5 = address.StartsWith("B") || address.StartsWith("b");
					if (flag5)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 12288).ToString());
					}
					bool flag6 = address.StartsWith("M") || address.StartsWith("m");
					if (flag6)
					{
						return OperateResult.CreateSuccessResult<string>(str + Convert.ToInt32(address.Substring(1)).ToString());
					}
				}
				else
				{
					bool flag7 = address.StartsWith("D") || address.StartsWith("d");
					if (flag7)
					{
						return OperateResult.CreateSuccessResult<string>(str + Convert.ToInt32(address.Substring(1)).ToString());
					}
					bool flag8 = address.StartsWith("R") || address.StartsWith("r");
					if (flag8)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 12288).ToString());
					}
				}
				result = new OperateResult<string>(StringResources.Language.NotSupportedDataType);
			}
			catch (Exception ex)
			{
				result = new OperateResult<string>(ex.Message);
			}
			return result;
		}
	}
}
